Ext.define('StaffServer.controller.TeacherController', {
	extend : 'Ext.app.Controller',

	models: ['TeacherModel'],
	stores: ['TeacherStore'],
	views:  ['teacherinfo.TeacherInfoGrid', 
	         
	         'teacherinfo.TeacherInfoMngr', 
	         'teacherinfo.TeacherInfoSearchPanel', 
	         'teacherinfo.TeacherInfoForm', 
	         'teacherinfo.TeacherInfoWindow', 
	         'teacherinfo.TeacherInfoExcelForm', 
	         'teacherinfo.TeacherInfoExcelWindow'],
	
	tabView: 'StaffServer.view.teacherinfo.TeacherInfoMngr',
	
	teacherInfoWindow:null,
	teacherInfoForm:null,
	teacherInfoExcelWindow:null,
	teacherInfoExcelForm:null,
	submitFlag: null,
	retireId: null,
	
	refs : [ {
		ref : 'teacherInfoSearchPanel',
		selector : 'teacherInfoSearchPanel'
	}, {
		ref : 'teacherInfoGrid',
		selector : 'teacherInfoGrid'
	}],

	// 初始化执行语句
	init : function() {
		teacherInfoWindow = Ext.widget("teacherInfoWindow");
		teacherInfoForm = Ext.ComponentQuery.query('teacherInfoForm')[0];
		teacherInfoExcelWindow = Ext.widget("teacherInfoExcelWindow");
		teacherInfoExcelForm = Ext.ComponentQuery.query('teacherInfoExcelForm')[0];
		
		// 控制事件
		this.control({
			'teacherInfoSearchPanel button[action=search]' : {
				click : this.search
			},
			'teacherInfoSearchPanel button[action=reset]' : {
				click : this.reset
			},
			'teacherInfoGrid button[action=saveTeacher]' : {
				click : this.saveTeacher
			},
			'teacherInfoGrid button[action=updateTeacher]' : {
				click : this.updateTeacher
			},
			'teacherInfoGrid button[action=deleteTeacher]' : {
				click : this.deleteTeacher
			},
			'teacherInfoGrid button[action=importExcel]' : {
				click : this.importExcel
			},
			'teacherInfoGrid button[action=exportExcel]' : {
				click : this.exportExcel
			},
			'teacherInfoWindow button[action=submit]' : {
				click : this.submitWindow
			},
			'teacherInfoWindow button[action=close]' : {
				click : this.closeWindow
			},
			'teacherInfoExcelWindow button[action=submit]' : {
				click : this.submitExcelWindow
			},
			'teacherInfoExcelWindow button[action=close]' : {
				click : this.closeExcelWindow
			},
			'teacherInfoGrid': {
				itemdblclick: this.updateTeacher,
				itemclick:this.opteacher
			}
		});
	},

	opteacher:function(a,b,c,d,e,f){
		var target = Ext.get(e.getTarget()); //获取事件对象
		if(target.hasCls('staffid')){
			this.updateTeacher();	
		} 
	},
	
	//searchPanel查询
	search : function() {
		var searchPanel = this.getTeacherInfoSearchPanel();
		var grid = this.getTeacherInfoGrid();
		grid.store.on('beforeload', function(store){
			var searchParams = searchPanel.getForm().getFieldValues();
			Ext.apply(store.proxy.extraParams, searchParams);
        });
		Ext.ComponentQuery.query('teacherInfoGrid > pagingtoolbar')[0].moveFirst();
	},
	
	//searchPanel重置
	reset: function(){
		var searchPanel = this.getTeacherInfoSearchPanel();
		searchPanel.getForm().reset();
	},
	
	//新增
	saveTeacher: function(){
	
		submitFlag = "save";
		teacherInfoForm.getForm().reset();
		teacherInfoWindow.setTitle("新增教师信息");
		teacherInfoWindow.show();
		 var myphoto= Ext.ComponentQuery.query('teacherInfoForm [name="teacherpic"]')[0];    
	        myphoto.getEl().dom.src ='pic/noteacher.jpg';
		Ext.ComponentQuery.query('teacherInfoForm field[name="staffId"]')[0].setReadOnly(false);
	},
	
	//更新
	updateTeacher: function(){
		var record = this.getTeacherInfoGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        Ext.ComponentQuery.query('teacherInfoForm field[name="staffId"]')[0].setReadOnly(true);
        teacherInfoForm.loadRecord(record);
  

        submitFlag = "update";
        teacherInfoWindow.setTitle("修改教师信息");
        teacherInfoWindow.show();
        var myphoto= Ext.ComponentQuery.query('teacherInfoForm [name="teacherpic"]')[0];    
        myphoto.getEl().dom.src ='pic/teacher'+record.data.staffId+'.jpg';
	},
	
	
	//删除
	deleteTeacher: function(){
		var $this = this;
		var record = this.getTeacherInfoGrid().getSelectionModel().getLastSelected();
        if (Ext.isEmpty(record)) {
            Ext.Msg.alert('提示', '请先选择要操作的记录!');
            return;
        }
        Ext.MessageBox.confirm('请确认', '确认删除吗?', function(btn, text){
            if (btn == 'yes') {
                showWaitMsg(); 
                Ext.Ajax.request({
                    url: 'teacher/deleteTeacher',
                    params: {
                    	staffId: record.get("staffId")
                    },
                    success: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        if (resp.success) {
                            Ext.Msg.alert('提示', '操作成功', function(){
                            	$this.search();
                            });
                        }
                        else {
                            Ext.Msg.alert('提示', resp.error);
                        }
                    },
                    failure: function(response){
                    	var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
                    }
                });
            }
        });
	},
	
	//提交表单
	submitWindow: function(){
		if (!teacherInfoForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = teacherInfoForm.getValues();
		teacherInfoForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: submitFlag == 'save' ? 'teacher/saveTeacher' : 'teacher/updateTeacher',
		    params: {    	values: values
		    },
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//关闭窗口
	closeWindow: function(){
		teacherInfoForm.getForm().reset();
		teacherInfoWindow.hide();
	},
	
	
	//导入
	importExcel: function(){
		teacherInfoExcelWindow.setTitle("导入文件");
		teacherInfoExcelWindow.show();
	},
	
	
	//提交表单
	submitExcelWindow: function(){
		if (!teacherInfoExcelForm.form.isValid()) {
            return;
        }
		var $this = this;
		var values = teacherInfoExcelForm.getValues();
		teacherInfoExcelForm.getForm().submit({
			waitTitle: '系统提示',   
  			waitMsg: '正在处理,请稍候', 
			clientValidation: true,
		    url: 'teacher/importExcel',
		    params: {
		    	values: values
		    },
		    
		    success: function(form, action) {
		    	Ext.Msg.alert('提示', "操作成功", function(){
		    		$this.closeExcelWindow();
		    		$this.search();
		    	});
		    },
		    failure: function(form, action) {
		    	var resp = Ext.JSON.decode(action.response.responseText);
		    	Ext.Msg.alert('提示', resp.error);
		    }
		});
	},
	
	//关闭窗口
	closeExcelWindow: function(){
		teacherInfoExcelForm.getForm().reset();
		teacherInfoExcelWindow.hide();
	},
	
	
	exportExcel: function(){
		var searchPanel = this.getTeacherInfoSearchPanel();
		var searchParams = searchPanel.getForm().getFieldValues();
		var jsonFilter = Ext.JSON.encode(searchParams);
		window.location.href = 'teacher/exportTeacherExcel?jsonFilter=' + jsonFilter;
	}
	
});